{
 "metadata": {
  "name": "",
  "signature": "sha256:a144a882df0b168305dd22b779ee9f996375da0b2979c0c26a19c787c55767b2"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "heading",
     "level": 1,
     "metadata": {},
     "source": [
      "NumPy Basics: Arrays and Vectorized Computation"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%matplotlib inline"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from __future__ import division\n",
      "from numpy.random import randn\n",
      "import numpy as np\n",
      "np.set_printoptions(precision=4, suppress=True)\n",
      "%cd ../book_scripts"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "The NumPy ndarray: a multidimensional array object"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data = randn(2, 3)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data\n",
      "data * 10\n",
      "data + data"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data.shape\n",
      "data.dtype"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Creating ndarrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data1 = [6, 7.5, 8, 0, 1]\n",
      "arr1 = np.array(data1)\n",
      "arr1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data2 = [[1, 2, 3, 4], [5, 6, 7, 8]]\n",
      "arr2 = np.array(data2)\n",
      "arr2\n",
      "arr2.ndim\n",
      "arr2.shape"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr1.dtype\n",
      "arr2.dtype"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.zeros(10)\n",
      "np.zeros((3, 6))\n",
      "np.empty((2, 3, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.arange(15)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Data Types for ndarrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr1 = np.array([1, 2, 3], dtype=np.float64)\n",
      "arr2 = np.array([1, 2, 3], dtype=np.int32)\n",
      "arr1.dtype\n",
      "arr2.dtype"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.array([1, 2, 3, 4, 5])\n",
      "arr.dtype\n",
      "float_arr = arr.astype(np.float64)\n",
      "float_arr.dtype"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.array([3.7, -1.2, -2.6, 0.5, 12.9, 10.1])\n",
      "arr\n",
      "arr.astype(np.int32)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "numeric_strings = np.array(['1.25', '-9.6', '42'], dtype=np.string_)\n",
      "numeric_strings.astype(float)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "int_array = np.arange(10)\n",
      "calibers = np.array([.22, .270, .357, .380, .44, .50], dtype=np.float64)\n",
      "int_array.astype(calibers.dtype)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "empty_uint32 = np.empty(8, dtype='u4')\n",
      "empty_uint32"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Operations between arrays and scalars"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
      "arr\n",
      "arr * arr\n",
      "arr - arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "1 / arr\n",
      "arr ** 0.5"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Basic indexing and slicing"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(10)\n",
      "arr\n",
      "arr[5]\n",
      "arr[5:8]\n",
      "arr[5:8] = 12\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr_slice = arr[5:8]\n",
      "arr_slice[1] = 12345\n",
      "arr\n",
      "arr_slice[:] = 64\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
      "arr2d[2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d[0][2]\n",
      "arr2d[0, 2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr3d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])\n",
      "arr3d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr3d[0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "old_values = arr3d[0].copy()\n",
      "arr3d[0] = 42\n",
      "arr3d\n",
      "arr3d[0] = old_values\n",
      "arr3d"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr3d[1, 0]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 4,
     "metadata": {},
     "source": [
      "Indexing with slices"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[1:6]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d\n",
      "arr2d[:2]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d[:2, 1:]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d[1, :2]\n",
      "arr2d[2, :1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d[:, :1]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr2d[:2, 1:] = 0"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Boolean indexing"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n",
      "data = randn(7, 4)\n",
      "names\n",
      "data"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "names == 'Bob'"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data[names == 'Bob']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data[names == 'Bob', 2:]\n",
      "data[names == 'Bob', 3]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "names != 'Bob'\n",
      "data[-(names == 'Bob')]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "mask = (names == 'Bob') | (names == 'Will')\n",
      "mask\n",
      "data[mask]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data[data < 0] = 0\n",
      "data"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "data[names != 'Joe'] = 7\n",
      "data"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Fancy indexing"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.empty((8, 4))\n",
      "for i in range(8):\n",
      "    arr[i] = i\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[[4, 3, 0, 6]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[[-3, -5, -7]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# more on reshape in Chapter 12\n",
      "arr = np.arange(32).reshape((8, 4))\n",
      "arr\n",
      "arr[[1, 5, 7, 2], [0, 3, 1, 2]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[[1, 5, 7, 2]][:, [0, 3, 1, 2]]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr[np.ix_([1, 5, 7, 2], [0, 3, 1, 2])]"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Transposing arrays and swapping axes"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(15).reshape((3, 5))\n",
      "arr\n",
      "arr.T"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.random.randn(6, 3)\n",
      "np.dot(arr.T, arr)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(16).reshape((2, 2, 4))\n",
      "arr\n",
      "arr.transpose((1, 0, 2))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr\n",
      "arr.swapaxes(1, 2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Universal Functions: Fast element-wise array functions"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(10)\n",
      "np.sqrt(arr)\n",
      "np.exp(arr)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = randn(8)\n",
      "y = randn(8)\n",
      "x\n",
      "y\n",
      "np.maximum(x, y) # element-wise maximum"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(7) * 5\n",
      "np.modf(arr)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Data processing using arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "points = np.arange(-5, 5, 0.01) # 1000 equally spaced points\n",
      "xs, ys = np.meshgrid(points, points)\n",
      "ys"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from matplotlib.pyplot import imshow, title"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "import matplotlib.pyplot as plt\n",
      "z = np.sqrt(xs ** 2 + ys ** 2)\n",
      "z\n",
      "plt.imshow(z, cmap=plt.cm.gray); plt.colorbar()\n",
      "plt.title(\"Image plot of $\\sqrt{x^2 + y^2}$ for a grid of values\")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "plt.draw()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Expressing conditional logic as array operations"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])\n",
      "yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])\n",
      "cond = np.array([True, False, True, True, False])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = [(x if c else y)\n",
      "          for x, y, c in zip(xarr, yarr, cond)]\n",
      "result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "result = np.where(cond, xarr, yarr)\n",
      "result"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(4, 4)\n",
      "arr\n",
      "np.where(arr > 0, 2, -2)\n",
      "np.where(arr > 0, 2, arr) # set only positive values to 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Not to be executed\n",
      "\n",
      "result = []\n",
      "for i in range(n):\n",
      "    if cond1[i] and cond2[i]:\n",
      "        result.append(0)\n",
      "    elif cond1[i]:\n",
      "        result.append(1)\n",
      "    elif cond2[i]:\n",
      "        result.append(2)\n",
      "    else:\n",
      "        result.append(3)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Not to be executed\n",
      "\n",
      "np.where(cond1 & cond2, 0,\n",
      "         np.where(cond1, 1,\n",
      "                  np.where(cond2, 2, 3)))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# Not to be executed\n",
      "\n",
      "result = 1 * cond1 + 2 * cond2 + 3 * -(cond1 | cond2)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Mathematical and statistical methods"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.random.randn(5, 4) # normally-distributed data\n",
      "arr.mean()\n",
      "np.mean(arr)\n",
      "arr.sum()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr.mean(axis=1)\n",
      "arr.sum(0)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])\n",
      "arr.cumsum(0)\n",
      "arr.cumprod(1)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Methods for boolean arrays"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(100)\n",
      "(arr > 0).sum() # Number of positive values"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "bools = np.array([False, False, True, False])\n",
      "bools.any()\n",
      "bools.all()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Sorting"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(8)\n",
      "arr\n",
      "arr.sort()\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = randn(5, 3)\n",
      "arr\n",
      "arr.sort(1)\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "large_arr = randn(1000)\n",
      "large_arr.sort()\n",
      "large_arr[int(0.05 * len(large_arr))] # 5% quantile"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Unique and other set logic"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "names = np.array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'])\n",
      "    np.unique(names)\n",
      "ints = np.array([3, 3, 3, 2, 2, 1, 1, 4, 4])\n",
      "np.unique(ints)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "sorted(set(names))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "values = np.array([6, 0, 0, 3, 2, 5, 6])\n",
      "np.in1d(values, [2, 3, 6])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "File input and output with arrays"
     ]
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Storing arrays on disk in binary format"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.arange(10)\n",
      "np.save('some_array', arr)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.load('some_array.npy')"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.savez('array_archive.npz', a=arr, b=arr)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arch = np.load('array_archive.npz')\n",
      "arch['b']"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!rm some_array.npy\n",
      "!rm array_archive.npz"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Saving and loading text files"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "!cat array_ex.txt"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "arr = np.loadtxt('array_ex.txt', delimiter=',')\n",
      "arr"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Linear algebra"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "x = np.array([[1., 2., 3.], [4., 5., 6.]])\n",
      "y = np.array([[6., 23.], [-1, 7], [8, 9]])\n",
      "x\n",
      "y\n",
      "x.dot(y)  # equivalently np.dot(x, y)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.dot(x, np.ones(3))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.random.seed(12345)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from numpy.linalg import inv, qr\n",
      "X = randn(5, 5)\n",
      "mat = X.T.dot(X)\n",
      "inv(mat)\n",
      "mat.dot(inv(mat))\n",
      "q, r = qr(mat)\n",
      "r"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Random number generation"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "samples = np.random.normal(size=(4, 4))\n",
      "samples"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from random import normalvariate\n",
      "N = 1000000\n",
      "%timeit samples = [normalvariate(0, 1) for _ in xrange(N)]\n",
      "%timeit np.random.normal(size=N)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 2,
     "metadata": {},
     "source": [
      "Example: Random Walks"
     ]
    },
    {
     "cell_type": "raw",
     "metadata": {},
     "source": [
      "import random\n",
      "position = 0\n",
      "walk = [position]\n",
      "steps = 1000\n",
      "for i in xrange(steps):\n",
      "    step = 1 if random.randint(0, 1) else -1\n",
      "    position += step\n",
      "    walk.append(position)"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "np.random.seed(12345)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nsteps = 1000\n",
      "draws = np.random.randint(0, 2, size=nsteps)\n",
      "steps = np.where(draws > 0, 1, -1)\n",
      "walk = steps.cumsum()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "walk.min()\n",
      "walk.max()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "(np.abs(walk) >= 10).argmax()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "heading",
     "level": 3,
     "metadata": {},
     "source": [
      "Simulating many random walks at once"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "nwalks = 5000\n",
      "nsteps = 1000\n",
      "draws = np.random.randint(0, 2, size=(nwalks, nsteps)) # 0 or 1\n",
      "steps = np.where(draws > 0, 1, -1)\n",
      "walks = steps.cumsum(1)\n",
      "walks"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "walks.max()\n",
      "walks.min()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "hits30 = (np.abs(walks) >= 30).any(1)\n",
      "hits30\n",
      "hits30.sum() # Number that hit 30 or -30"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "crossing_times = (np.abs(walks[hits30]) >= 30).argmax(1)\n",
      "crossing_times.mean()"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "steps = np.random.normal(loc=0, scale=0.25,\n",
      "                         size=(nwalks, nsteps))"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    }
   ],
   "metadata": {}
  }
 ]
}